package com.woollen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woollen.mapper.MemberMapper;
import com.woollen.model.Member;
import com.woollen.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.hutool.crypto.SecureUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Member login(String phoneOrEmail, String password) {
        QueryWrapper<Member> qw = new QueryWrapper<>();
        if (phoneOrEmail.contains("@")) {
            qw.eq("email", phoneOrEmail);
        } else {
            qw.eq("phone", phoneOrEmail);
        }
        // 管理员身份判断统一用 role 字段
        qw.eq("role", "管理员");
        Member admin = memberMapper.selectOne(qw);
        String md5Pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        if (admin == null || !md5Pwd.equals(admin.getPassword())) {
            throw new RuntimeException("管理员账号或密码错误");
        }
        admin.setPassword(null); // 脱敏
        return admin;
    }
} 